package instance

import (
	"context"

	"gitee.com/shiwjlinux/arm/common"
	"gitee.com/shiwjlinux/arm/ioc"
)

func GetService() Service {
	return ioc.Controller.Get(AppName).(Service)
}

type Service interface {
	CreateInstance(context.Context, *Instance) (*Instance, error)
	DeleteInstance(context.Context, *DeleteInstanceRequest) (*Instance, error)
	DescribeInstance(context.Context, *DescribeInstanceRequest) (*Instance, error)
	// UpdateInstance(context.Context, *UpdateInstanceRequest) (*Instance, error)
	QueryInstance(context.Context, *QueryInstanceRequest) (*InstanceSet, error)
	UpdateHeartbeat(context.Context, *UpdateHeartbeatRequest) (*Instance, error)

	UpdateHeartbeats(context.Context, *UpdateHeartbeatRequests) error
}

type (
	DeleteInstanceRequest struct {
		IDs []string `json:"IDs"`
	}

	DescribeInstanceRequest struct {
		ID string `json:"ID" bson:"_id"`
	}

	QueryInstanceRequest struct {
		ID       string            `json:"ID" bson:"_id"`
		Sn       string            `json:"sn" bson:"sn"`
		Keywords string            `json:"keywords" bson:"keywords"`
		Tags     map[string]string `json:"tag" bson:"tag"`
		*common.PageRequest
	}
	UpdateInstanceRequest struct {
		ID string `json:"ID" bson:"_id"`
		*CreateInstanceRequest
	}
	UpdateHeartbeatRequest struct {
		ID string `json:"ID" bson:"_id"`
		*Heartbeat
	}

	UpdateHeartbeatRequests struct {
		IDs []string
		*Heartbeat
	}
)

func NewQueryInstanceRequest() *QueryInstanceRequest {
	return &QueryInstanceRequest{
		Tags:        make(map[string]string),
		PageRequest: common.NewPageRequest(),
	}
}

func (q *QueryInstanceRequest) WithID(id string) *QueryInstanceRequest {
	q.ID = id
	return q
}
func (q *QueryInstanceRequest) WithSN(sn string) *QueryInstanceRequest {
	q.Sn = sn
	return q
}

func NewDescribeInstanceRequest(id string) *DescribeInstanceRequest {
	return &DescribeInstanceRequest{
		ID: id,
	}
}

func NewDeleteInstanceRequest(ids []string) *DeleteInstanceRequest {
	return &DeleteInstanceRequest{
		IDs: ids,
	}
}

func NewUpdateInstanceRequest(id string) *UpdateInstanceRequest {
	return &UpdateInstanceRequest{
		ID: id,
	}
}

func NewUpdateHeartbeatRequest(id string) *UpdateHeartbeatRequest {
	heartBeat := NewHeartbeat()
	return &UpdateHeartbeatRequest{
		ID:        id,
		Heartbeat: heartBeat,
	}
}

func NewUpdateHeartbeatRequests(ids []string) *UpdateHeartbeatRequests {
	heartBeat := NewHeartbeat()
	return &UpdateHeartbeatRequests{
		IDs:       ids, // 假设 UpdateHeartbeatRequest 结构体中有一个 IDs 字段
		Heartbeat: heartBeat,
	}
}
